package adventure.commands.admin;

import adventure.*;
import adventure.commands.*;
import adventure.placeable.Mobile;
import adventure.placeable.PlaceableManager;
import colour.ColourUtil;
import player.*;

/**
 *
 * @author Michael Hanns
 *
 */
public class SpawnMobile extends AbstractCommand {

    PlaceableManager placeables;

    public SpawnMobile(Adventure a) {
        super(a, new String[]{"SPAWNMOBILE"});
        placeables = (PlaceableManager) ((AdventureEdit) a).getPlaceableManager();
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (p.isAdmin()) {
            if (args.length == 1) {
                return listMobiles(p);
            } else if (args.length == 2) {
                if (isInteger(args[1])) {
                    return spawnMobile(p, 1, Integer.parseInt(args[1]));
                }
                return "Spawn which mobile?\nSyntax: spawnmobile [[<quantity>] <ID>]";
            } else if (args.length == 3) {
                if (isInteger(args[1])) {
                    if (isInteger(args[2])) {
                        return spawnMobile(p, Integer.parseInt(args[1]), Integer.parseInt(args[2]));
                    }
                    return "Spawn which mobile?\nSyntax: spawnmobile [[<quantity>] <ID>]";
                }

                return "Spawn which mobile?\nSyntax: spawnmobile [[<quantity>] <ID>]";
            } else {
                return "Spawn which mobile?\nSyntax: spawnmobile [[<quantity>] <ID>]";
            }
        } else {
            return "Command not recognised.";
        }
    }

    private String listMobiles(Player p) {

        int c = 1;
        Mobile mobile;
        String divider = ColourUtil.colourise(
                " +----------------------------------------------------------------------------+",
                ColourUtil.darkRed);

        String tableOut = divider
                + "\n"
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("ID  ", ColourUtil.red)
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("Name            ", ColourUtil.red)
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("Lvl", ColourUtil.red)
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("Atk", ColourUtil.red)
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("Def", ColourUtil.red)
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("Spd", ColourUtil.red)
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("Lck", ColourUtil.red)
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("Agg", ColourUtil.red)
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("Vendor", ColourUtil.red)
                + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                + ColourUtil.colourise("Imm", ColourUtil.red)
                + ColourUtil.colourise(" |", ColourUtil.darkRed)
                + "\n"
                + divider;

        while ((mobile = placeables.getMobile(c)).getID() != 0) {
            tableOut +=
                    "\n"
                    + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                    + ColourUtil.colourise(addWhiteSpace(mobile.getID() + "", 4), ColourUtil.red)
                    + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                    + ColourUtil.colourise(addWhiteSpace(mobile.getName(), 16), ColourUtil.red)
                    + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                    + ColourUtil.colourise(addWhiteSpace(mobile.getLevel() + "", 3), ColourUtil.red)
                    + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                    + ColourUtil.colourise(addWhiteSpace(mobile.getAttack() + "", 3), ColourUtil.red)
                    + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                    + ColourUtil.colourise(addWhiteSpace(mobile.getDefence() + "", 3), ColourUtil.red)
                    + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                    + ColourUtil.colourise(addWhiteSpace(mobile.getSpeed() + "", 3), ColourUtil.red)
                    + ColourUtil.colourise(" | ", ColourUtil.darkRed)
                    + ColourUtil.colourise(addWhiteSpace(mobile.getLuck() + "", 3), ColourUtil.red)
                    + ColourUtil.colourise(" | ", ColourUtil.darkRed);

            if (mobile.isAggro()) {
                tableOut += ColourUtil.colourise(addWhiteSpace("Yes", 3), ColourUtil.red);
            } else {
                tableOut += ColourUtil.colourise(addWhiteSpace("No", 3), ColourUtil.red);
            }
            tableOut += ColourUtil.colourise(" | ", ColourUtil.darkRed);

            if (mobile.isVendor()) {
                tableOut += ColourUtil.colourise(addWhiteSpace(mobile.getVendorModifier() + "", 6), ColourUtil.red);
            } else {
                tableOut += ColourUtil.colourise(addWhiteSpace("No", 6), ColourUtil.red);
            }
            tableOut += ColourUtil.colourise(" | ", ColourUtil.darkRed);
            
            if (mobile.isImmortal()) {
                tableOut += ColourUtil.colourise(addWhiteSpace("Yes", 3), ColourUtil.red);
            } else {
                tableOut += ColourUtil.colourise(addWhiteSpace("No", 3), ColourUtil.red);
            }
            tableOut += ColourUtil.colourise(" |", ColourUtil.darkRed);

            c++;
        }

        tableOut += "\n" + divider;

        return tableOut;
    }

    private String addWhiteSpace(String input, int maxSize) {
        if (input.length() > maxSize) {
            input = input.substring(0, maxSize - 2) + "..";
        } else {
            while (input.length() < maxSize) {
                input += " ";
            }

        }
        return ColourUtil.colourise(input, ColourUtil.red);
    }

    private String spawnMobile(Player p, int quant, int id) {
        if (quant > 0) {
            Mobile mobile = placeables.getMobile(id);
            int x = p.x();
            int y = p.y();
            int z = p.z();

            if (mobile.getID() > 0) {
                String quantOut;
                if (quant > 1) {
                    quantOut = quant + " ";
                } else {
                    quantOut = "a ";
                }

                for (int c = 0; c < quant; c++) {
                    rooms.addMobileToRoom(mobile.cloneThis(), x, y, z);
                }

                out.broadcastLocalToPlayer(p,
                        ColourUtil.colourise(p.getAliveName(), ColourUtil.player)
                        + " has spawned "
                        + quantOut
                        + ColourUtil.colourise(mobile.getName(), ColourUtil.mobile)
                        + ".");

                return "You have spawned "
                        + quantOut
                        + ColourUtil.colourise(mobile.getName(), ColourUtil.mobile)
                        + ".";

            }
            return "That is not a valid mobile ID! Type 'spawnmobile' to view a list of all mobiles.";
        }
        return "You must specify a quantity of 1 or more, or leave quantity blank!\nSyntax: spawnmobile [[<quantity>] <ID>]";
    }

    private boolean isInteger(String arg) {
        try {
            Integer.parseInt(arg);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
